﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;

namespace TinyEngine.Core.Services
{
    public class InputService : ITinyService
    {
        GamePadState previousPadState;
        GamePadState currentPadState;

        KeyboardInputState keyboardState = new KeyboardInputState();
        
        public KeyboardInputState KeyboardInput
        {
            get { return keyboardState; }
        }

        public void Register()
        {
            
        }

        public void UnRegister()
        {
            
        }

        public GamePadState CurrentPadState
        {
            get { return currentPadState; }
        }
        
        public void Update(float elapsed)
        {
            previousPadState = currentPadState;
            currentPadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);


            keyboardState.Update(elapsed);
            //keyboardState.Previous = keyboardState.Current;
            //keyboardState.Current = Keyboard.GetState();

        }
    }

    public class KeyboardInputState : InputState<KeyboardState>
    {
        public override void Update(float elapsed)
        {
            this.Previous = this.Current;
            this.Current = Keyboard.GetState();
        }

        public bool IsKeyDown(Keys leftKey)
        {
            return Current.IsKeyDown(leftKey);
        }

        public bool IsNewKeyPress(Keys leftKey)
        {
            return Current.IsKeyUp(leftKey) && Previous.IsKeyDown(leftKey);
        }
    }

    public abstract class InputState<T>
    {
        public T Previous { get; set; }
        public T Current { get; set; }
        public abstract void Update(float elapsed);
        
    }
}
